package com.zhzhinn.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.edgclub.edg.R;
import com.edgclub.edg.activity.ChatBlacklistActivity;
import com.edgclub.edg.activity.GroupMembersActivity;
import com.edgclub.edg.api.FriendApi;
import com.edgclub.edg.api.param.MessageParam;
import com.edgclub.edg.constant.BBConstant;
import com.edgclub.edg.event.BroadCastEvent;
import com.edgclub.edg.event.EventBus;
import com.edgclub.edg.fragment.BBFragment;
import com.edgclub.edg.fragment.GzBaseFragment;
import com.edgclub.edg.model.TopicChannel;
import com.edgclub.edg.utils.util.UserPrefUtils;
import com.squareup.otto.ThreadEnforcer;

import java.util.ArrayList;


public class ZhanMainFragment extends GzBaseFragment implements View.OnClickListener{
    private ViewPager pager;
    private MyPagerAdapter adapter;

    private ArrayList<GzBaseFragment> fragmentList;

    private RelativeLayout room_layout , inn_layout;

    private TextView room_text , inn_text;

    public static ZhanMainFragment newInstance(String text) {
        ZhanMainFragment f = new ZhanMainFragment();
        return f;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.zz_fragment_zhan, container, false);
        initView(view);
        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initData();
    }
    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getEventBus(BroadCastEvent.EVENTBUS_COMMON,
                ThreadEnforcer.MAIN).register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getEventBus(BroadCastEvent.EVENTBUS_COMMON,
                ThreadEnforcer.MAIN).unregister(this);
    }

    private void initView(View view){
//        initLoadView(view);
        pager = (ViewPager) view.findViewById(R.id.viewpager2);
        room_layout = (RelativeLayout) view.findViewById(R.id.room_layout);
        room_layout.setOnClickListener(this);
        inn_layout = (RelativeLayout) view.findViewById(R.id.inn_layout);
        inn_layout.setOnClickListener(this);
        room_text = (TextView) view.findViewById(R.id.room_text);
        inn_text = (TextView) view.findViewById(R.id.inn_text);
    }

    private void initData(){
        fragmentList = new ArrayList<>();
        TopicChannel channelTmp = new TopicChannel();
        channelTmp.setChannelId(BBConstant.channel_all);
        channelTmp.setTitle("全部");
        TopicChannel channelTmp1 = new TopicChannel();
        channelTmp1.setChannelId(BBConstant.channel_attention);
        channelTmp1.setTitle("我关注的");
        fragmentList.add(RoomFragment.newInstance(BBConstant.TOPIC_TYPE_CHANNEL, channelTmp));
        fragmentList.add(InnFragment.newInstance(BBConstant.TOPIC_TYPE_CHANNEL, channelTmp1));

        adapter = new MyPagerAdapter(getChildFragmentManager());
        adapter.setFragmentList(fragmentList);
        pager.setAdapter(adapter);
        final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources()
                .getDisplayMetrics());
        pager.setPageMargin(pageMargin);
        pager.setCurrentItem(0);
        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        pager.setCurrentItem(0);
                        room_text.setTextColor(getResources().getColor(R.color.c1));
                        inn_text.setTextColor(getResources().getColor(R.color.c3));
                        break;
                    case 1:
                        pager.setCurrentItem(1);
                        room_text.setTextColor(getResources().getColor(R.color.c3));
                        inn_text.setTextColor(getResources().getColor(R.color.c1));
                        break;
                    default:
                        break;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.room_layout:
                pager.setCurrentItem(0);
                room_text.setTextColor(getResources().getColor(R.color.c1));
                inn_text.setTextColor(getResources().getColor(R.color.c3));
                break;
            case R.id.inn_layout:
                pager.setCurrentItem(1);
                room_text.setTextColor(getResources().getColor(R.color.c3));
                inn_text.setTextColor(getResources().getColor(R.color.c1));
                break;
            default:
                break;
        }
    }

    public class MyPagerAdapter extends FragmentPagerAdapter {

        ArrayList<GzBaseFragment> fragmentList;

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

//        @Override
//        public CharSequence getPageTitle(int position) {
//            return list.get(position).getTitle();
//        }

        @Override
        public int getCount() {
            return fragmentList.size();
        }

        @Override
        public GzBaseFragment getItem(int position) {
            return fragmentList.get(position);
        }

        public void setFragmentList(ArrayList<GzBaseFragment> fragmentList) {
            this.fragmentList = fragmentList;
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

    }

}

